﻿namespace ACCOUNTING
{
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;

    [StandardModule]
    internal sealed class ConvertModule
    {
        public static string AmtInWord(decimal Num)
        {
            string str4;
            string str2 = Conversions.ToString(Num);
            if (Strings.InStr(1, str2, ".", CompareMethod.Binary) != 0)
            {
                string expression = Strings.Mid(str2, Strings.InStr(1, str2, ".", CompareMethod.Binary) + 1);
                if (Strings.Len(expression) == 1)
                {
                    expression = expression + "0";
                }
                if (Strings.Len(expression) > 2)
                {
                    expression = Strings.Mid(expression, 1, 2);
                }
                str4 = Conversions.ToString(Operators.AddObject(NumToWord(new decimal(Conversions.ToDouble(Strings.Mid(str2, 1, Strings.InStr(1, str2, ".", CompareMethod.Binary) - 1)))), Interaction.IIf(Conversions.ToDouble(expression) > 0.0, " and Cents " + cWord3(new decimal(Conversions.ToDouble(expression))), "")));
            }
            else
            {
                str4 = NumToWord(new decimal(Conversions.ToDouble(str2)));
            }
            return (str4 + " Only");
        }

        public static string cWord3(decimal Num)
        {
            string expression = "";
            string str4 = "";
            if (decimal.Compare(Num, decimal.Zero) < 0)
            {
                Num = decimal.Multiply(Num, decimal.MinusOne);
            }
            expression = Conversions.ToString(Num);
            if (Strings.Len(expression) == 3)
            {
                str4 = retWord(Conversions.ToDecimal(Conversions.ToString(Conversions.ToDouble(Strings.Mid(expression, 1, 1))))) + " Hundred";
                expression = Strings.Mid(expression, 2, Strings.Len(expression));
            }
            if (Strings.Len(expression) <= 2)
            {
                if ((Conversions.ToDouble(expression) >= 0.0) & (Conversions.ToDouble(expression) <= 20.0))
                {
                    str4 = str4 + " " + retWord(new decimal(Conversions.ToDouble(expression)));
                }
                else
                {
                    str4 = str4 + " " + retWord(new decimal(Conversions.ToDouble(Strings.Mid(expression, 1, 1) + "0"))) + " " + retWord(new decimal(Conversions.ToDouble(Strings.Mid(expression, 2, 1))));
                }
            }
            expression = Conversions.ToString(Num);
            return str4;
        }

        public static string cWordG3(decimal Num)
        {
            string expression = "";
            string str4 = "";
            string str2 = "";
            expression = Conversions.ToString(Num);
            if ((Strings.Len(expression) % 2) != 0)
            {
                str2 = Conversions.ToString(Conversions.ToDouble(Strings.Mid(expression, 1, 1)));
                if (str2 != "0")
                {
                    str4 = retWord(Conversions.ToDecimal(str2));
                    str2 = Conversions.ToString(Conversions.ToDouble("1" + strReplicate("0", Strings.Len(expression) - 1) + "000"));
                    str4 = str4 + " " + retWord(Conversions.ToDecimal(str2));
                }
                expression = Strings.Mid(expression, 2);
            }
            while (Strings.Len(expression) != 0)
            {
                str2 = Conversions.ToString(Conversions.ToDouble(Strings.Mid(expression, 1, 2)));
                if (str2 != "0")
                {
                    str4 = str4 + " " + cWord3(Conversions.ToDecimal(str2));
                    str2 = Conversions.ToString(Conversions.ToDouble("1" + strReplicate("0", Strings.Len(expression) - 2) + "000"));
                    str4 = str4 + " " + retWord(Conversions.ToDecimal(str2));
                }
                expression = Strings.Mid(expression, 3);
            }
            return str4;
        }

        public static string NumToWord(decimal Num)
        {
            string expression = Conversions.ToString(Num);
            if (Strings.Len(expression) <= 3)
            {
                return cWord3(new decimal(Conversions.ToDouble(expression)));
            }
            return (cWordG3(new decimal(Conversions.ToDouble(Strings.Mid(expression, 1, Strings.Len(expression) - 3)))) + " " + cWord3(new decimal(Conversions.ToDouble(Strings.Mid(expression, Strings.Len(expression) - 2)))));
        }

        public static string retWord(decimal Num)
        {
            object[,] array = new object[,] { 
                { 0, "" }, { 1, "One" }, { 2, "Two" }, { 3, "Three" }, { 4, "Four" }, { 5, "Five" }, { 6, "Six" }, { 7, "Seven" }, { 8, "Eight" }, { 9, "Nine" }, { 10, "Ten" }, { 11, "Eleven" }, { 12, "Twelve" }, { 13, "Thirteen" }, { 14, "Fourteen" }, { 15, "Fifteen" }, 
                { 0x10, "Sixteen" }, { 0x11, "Seventeen" }, { 0x12, "Eighteen" }, { 0x13, "Nineteen" }, { 20, "Twenty" }, { 30, "Thirty" }, { 40, "Forty" }, { 50, "Fifty" }, { 60, "Sixty" }, { 70, "Seventy" }, { 80, "Eighty" }, { 90, "Ninety" }, { 100, "Hundred" }, { 0x3e8, "Thousand" }, { 0x186a0, "Lakh" }, { 0x989680, "Crore" }
             };
            int num2 = Information.UBound(array, 1);
            for (int i = 0; i <= num2; i++)
            {
                if (Operators.ConditionalCompareObjectEqual(Num, array[i, 0], false))
                {
                    return Conversions.ToString(array[i, 1]);
                }
            }
            return "";
        }

        public static string strReplicate(string str, int intD)
        {
            string str2 = "";
            int num2 = intD;
            for (int i = 1; i <= num2; i++)
            {
                str2 = str2 + str;
            }
            return str2;
        }
    }
}

